// src/store/company.js
import { defineStore } from 'pinia'
import { companyApi } from '../api/company'

export const useCompanyStore = defineStore('company', {
    state: () => ({
        profile: null,
        positions: [],
        applications: [],
        pendingApplications: []
    }),

    actions: {
        // 获取企业信息
        async fetchProfile() {
            try {
                const response = await companyApi.getCompanyProfile()
                this.profile = response.data
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '获取企业信息失败')
            }
        },

        // 注册企业信息
        async registerCompanyProfile(data) {
            try {
                const response = await companyApi.registerCompany(data)
                this.profile = response.data
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '注册企业信息失败')
            }
        },

        // 更新企业信息
        async updateProfile(data) {
            try {
                const response = await companyApi.updateCompanyProfile(data)
                this.profile = response.data
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '更新企业信息失败')
            }
        },

        // 提交审核申请
        async submitForReview() {
            try {
                const response = await companyApi.submitReview()
                // 更新企业信息状态
                if (this.profile) {
                    this.profile.status = response.data.status
                }
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '提交审核失败')
            }
        },

        // 获取岗位列表
        async fetchPositions(params) {
            try {
                const response = await companyApi.getMyPositions(params)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '获取岗位列表失败')
            }
        },

        // 获取岗位详情
        async fetchPositionDetail(positionId) {
            try {
                const response = await companyApi.getPositionDetail(positionId)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '获取岗位详情失败')
            }
        },

        // 发布岗位
        async createPosition(data) {
            try {
                const response = await companyApi.createPosition(data)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '发布岗位失败')
            }
        },

        // 更新岗位
        async updatePosition(id, data) {
            try {
                const response = await companyApi.updatePosition(id, data)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '更新岗位失败')
            }
        },

        // 删除岗位
        async deletePosition(id) {
            try {
                const response = await companyApi.deletePosition(id)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '删除岗位失败')
            }
        },

        // 切换岗位状态
        async togglePositionStatus(id) {
            try {
                const response = await companyApi.togglePositionStatus(id)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '切换岗位状态失败')
            }
        },

        // 获取申请列表
        async fetchApplications(params) {
            try {
                const response = await companyApi.getCompanyApplications(params)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '获取申请列表失败')
            }
        },

        // 获取申请详情
        async fetchApplicationDetail(applicationId) {
            try {
                const response = await companyApi.getApplicationDetail(applicationId)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '获取申请详情失败')
            }
        },

        // 处理申请
        async processApplication(applicationId, data) {
            try {
                const response = await companyApi.processApplication(applicationId, data)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '处理申请失败')
            }
        },

        // 获取企业实习列表
        async fetchCompanyInternships(params) {
            try {
                const response = await companyApi.getCompanyInternships(params)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '获取实习列表失败')
            }
        },

        // 获取评价记录
        async fetchEvaluationRecords(params) {
            try {
                const response = await companyApi.getEvaluationRecords(params)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '获取评价记录失败')
            }
        },

        // 评定实习生
        async evaluateIntern(data) {
            try {
                const response = await companyApi.evaluateIntern(data)
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '评定实习生失败')
            }
        }
    }
})
